{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a58bc1a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "我是一名大学生，来自中国。我喜欢阅读、旅行和运动。我对不同的文化和语言都很感兴趣，所以我学习了英语、日语和西班牙语。我也很喜欢挑战自己，所以我经常参加各种活动和比赛。我也很注重自我发展，所以我会不断学习新知识和技能。我希望能够在未来成为一名成功的专业人士，并为社会做出贡献。\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from langchain.llms import OpenAI\n",
    "api_key=os.getenv(\"OPENAI_API_KEY\")\n",
    "llm=OpenAI(\n",
    "    model=\"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key=api_key\n",
    "    )\n",
    "re=llm.invoke(\"介绍一下你自己\")\n",
    "print(re)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8146161f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "import os\n",
    "from langchain.llms import OpenAI\n",
    "api_key=os.getenv(\"OPENAI_API_KEY\")\n",
    "llm=OpenAI(\n",
    "    model=\"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key=api_key\n",
    "    )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "40b6ad95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "函数名称：\"example\"\n",
      "源代码：\n",
      "def example():\n",
      "    print(\"hello,world.\")\n",
      "\n",
      "代码解释：\n",
      "这是一个名为example的函数，它没有任何参数，当被调用时，会打印出\"hello,world.\"这句话。\n"
     ]
    }
   ],
   "source": [
    "from langchain.prompts import StringPromptTemplate\n",
    "\n",
    "def example():\n",
    "    print(\"hello,world.\")\n",
    "PROMPT=\"\"\"\\\n",
    "你是一个有天赋和经验的程序员，现在给你函数名称，你会按照如下格式分别输出这段代码的名称，源代码，中文解释。\n",
    "函数名称：\"{function_name}\"\n",
    "源代码：\n",
    "{source_code}\n",
    "代码解释：\n",
    "\"\"\"\n",
    "import inspect\n",
    "\n",
    "def get_code(function_name):\n",
    "    return inspect.getsource(function_name)\n",
    "class MYprompts(StringPromptTemplate):\n",
    "    def format(self,**kwargs)->str:\n",
    "        source_code=get_code(kwargs[\"function_name\"])\n",
    "        prompt=PROMPT.format(\n",
    "            function_name=kwargs[\"function_name\"].__name__,source_code=source_code\n",
    "        )\n",
    "        return prompt\n",
    "\n",
    "M=MYprompts(input_variables=[\"function_name\"])\n",
    "prompt=M.format(function_name=example)\n",
    "A=llm.invoke(prompt)\n",
    "print(A)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ccd259b",
   "metadata": {},
   "source": [
    "根据长度选择提示词示例组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a66263b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "给出每个输入词的反义词\n",
      "\n",
      "原词:happy\n",
      "反义词:sad\n",
      "\n",
      "原词:sunny\n",
      "反义词:gloomy\n",
      "\n",
      "原词:big\n",
      "反义词:\n"
     ]
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.prompts import FewShotPromptTemplate\n",
    "from langchain.prompts.example_selector import LengthBasedExampleSelector\n",
    "from langchain.prompts import PipelinePromptTemplate\n",
    "PROMPT=[\n",
    "    {\"input\":\"happy\",\"output\":\"sad\"},\n",
    "    {\"input\":\"sunny\",\"output\":\"gloomy\"},\n",
    "    {\"input\":\"tall\",\"output\":\"short\"},\n",
    "    {\"input\":\"开心\",\"output\":\"伤心\"},\n",
    "    {\"input\":\"长\",\"output\":\"短\"},\n",
    "    ]\n",
    "example_prompt=PromptTemplate(\n",
    "    input_variables={\"input\",\"output\"},\n",
    "    template=\"原词:{input}\\n反义词:{output}\"\n",
    "    )\n",
    "example_selector=LengthBasedExampleSelector(\n",
    "    example_prompt=example_prompt,\n",
    "    examples=PROMPT,\n",
    "    max_length=5\n",
    "    )\n",
    "\n",
    "dynamic_prompt=FewShotPromptTemplate(\n",
    "    example_selector=example_selector,\n",
    "    example_prompt=example_prompt,\n",
    "    prefix=\"给出每个输入词的反义词\",\n",
    "    suffix=\"原词:{adject}\\n反义词:\",\n",
    "    input_variables=[\"adject\"]\n",
    "    )\n",
    "print(dynamic_prompt.format(adject=\"big\"))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "76762fbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting tiktoken\n",
      "  Using cached tiktoken-0.9.0-cp310-cp310-win_amd64.whl (894 kB)\n",
      "Collecting faiss-cpu\n",
      "  Downloading faiss_cpu-1.11.0-cp310-cp310-win_amd64.whl (15.0 MB)\n",
      "     --------------------------------------- 15.0/15.0 MB 38.6 MB/s eta 0:00:00\n",
      "Collecting regex>=2022.1.18\n",
      "  Using cached regex-2024.11.6-cp310-cp310-win_amd64.whl (274 kB)\n",
      "Requirement already satisfied: requests>=2.26.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from tiktoken) (2.32.4)\n",
      "Requirement already satisfied: numpy<3.0,>=1.25.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from faiss-cpu) (2.2.6)\n",
      "Requirement already satisfied: packaging in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from faiss-cpu) (24.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2.5.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2025.6.15)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests>=2.26.0->tiktoken) (3.10)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests>=2.26.0->tiktoken) (3.4.2)\n",
      "Installing collected packages: regex, faiss-cpu, tiktoken\n",
      "Successfully installed faiss-cpu-1.11.0 regex-2024.11.6 tiktoken-0.9.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip available: 22.2.2 -> 25.1.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "pip install tiktoken faiss-cpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d40903d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "给出每个输入词的反义词\n",
      "\n",
      "原词:tall\n",
      "反义词:short\n",
      "\n",
      "原词:happy\n",
      "反义词:sad\n",
      "\n",
      "原词:sunny\n",
      "反义词:gloomy\n",
      "\n",
      "原词:big\n",
      "反义词:\n"
     ]
    }
   ],
   "source": [
    "#向量相似度\n",
    "from langchain.prompts.example_selector import MaxMarginalRelevanceExampleSelector\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.vectorstores import FAISS\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain.prompts import FewShotPromptTemplate\n",
    "import os\n",
    "api_key=os.getenv(\"OPENAI_API_KEY\")\n",
    "PROMPT=[\n",
    "    {\"input\":\"happy\",\"output\":\"sad\"},\n",
    "    {\"input\":\"sunny\",\"output\":\"gloomy\"},\n",
    "    {\"input\":\"tall\",\"output\":\"short\"},\n",
    "    {\"input\":\"开心\",\"output\":\"伤心\"},\n",
    "    {\"input\":\"长\",\"output\":\"短\"},\n",
    "    ]\n",
    "example_prompt=PromptTemplate(\n",
    "    input_variables={\"input\",\"output\"},\n",
    "    template=\"原词:{input}\\n反义词:{output}\"\n",
    "    )\n",
    "example_selector=MaxMarginalRelevanceExampleSelector.from_examples(\n",
    "    PROMPT,\n",
    "    OpenAIEmbeddings(openai_api_key=api_key),\n",
    "    FAISS,\n",
    "    k=3\n",
    "    )\n",
    "mmr_prompt=FewShotPromptTemplate(\n",
    "    example_selector=example_selector,\n",
    "    example_prompt=example_prompt,\n",
    "    prefix=\"给出每个输入词的反义词\",\n",
    "    suffix=\"原词:{adject}\\n反义词:\",\n",
    "    input_variables=[\"adject\"]\n",
    "    )\n",
    "print(mmr_prompt.format(adject=\"big\"))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2141e4c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
